<?php
/**
 *
 * @author Martin "DevelX" Jurča
 */
class PaySec extends Module {
    protected $name = 'PaySec';
    protected $administrable = false;
    protected $version = '2010-05-06';
    protected $details = array('dependencies' => array(),
            'observes' => array('PaySec'));

    protected $gatewayURL = 'https://gateway.paysec.cz/gateway.aspx';
    protected $MicroaccountNumber = 8763986;
    protected $paymentType = 5;
    protected $wsdlURL = 'https://mapi.paysec.cz/?wsdl';
    protected $login = 'Pay8763986';
    protected $password = 'KQI2gwg';

    public function __construct(DBAPI $dbapi, RecordList $recordList) {
        parent::__construct($dbapi, $recordList);
    }

    public function install() {
        return true;
    }

    public function uninstall() {
        return true;
    }

    public function success($order, $key = false) {
        $user = Modules::getModule('Users')->getUser();
        $order = Modules::getModule('Orders')->getOrderData($order);
        $result = $this->checkPaymentStatus($order['id'], $order['amount']);
        if ($result == 0) {
            $this->addMsg('transactionSuccess', self::MSG_CONFIRM);
            Modules::getModule('Orders')->resolveOrder($order['id']);
        } elseif ($result == 1) {
            $this->addMsg('transactionRefused', self::MSG_CONFIRM);
        } elseif ($result == 5) {
            $this->addMsg('transactionImpossible', self::MSG_NOTICE);
        } else {
            $this->addMsg('transactionStatusUnknown', self::MSG_NOTICE);
        }
    }

    public function fail() {
        $this->addMsg("transactionFail", self::MSG_ERROR);
    }

    public function redirectToPayment($Amount, $credits, $uid, $Note) {
        $Amount = round((float) $Amount, 2);
        $params = array('credits' => $credits);
        $MessageForTarget = $this->lang('messageForTarget', $params);
        $MerchantOrderId = Modules::getModule('Orders')->
                createOrder($uid, $credits, $this->paymentType, $Note,
                $Amount);
        $url = 'http://www.' . Config::DOMAIN_NAME . Config::WEB_DIR .
                '/navstevnici/my-credits/';
        $BackURL = "{$url}success/PaySec/{0}/";
        $CancelURL = "{$url}fail/PaySec/";
        $Note = strlen($Note) > 100 ? substr($Note, 0, 100) : $Note;
        Kernel::setRedirect(null);
        $params = array('gatewayURL' => $this->gatewayURL,
                'MicroaccountNumber' => $this->MicroaccountNumber, 'Amount' =>
                $Amount, 'MerchantOrderId' => $MerchantOrderId,
                'MessageForTarget' => $MessageForTarget, 'BackURL' => $BackURL,
                'CancelURL' => $CancelURL);
        $this->template('redirect', $params);
        exit();
    }

    private function checkPaymentStatus($orderID, $amount) {
        try {
            $paysecMapi = new SoapClient($this->wsdlURL);
            $resultCode = $paysecMapi->VerifyTransactionIsPaid($this->login,
                    $this->password, $orderID, $amount);
            return $resultCode;
        } catch (Exception $e) {
            trigger_error($e->getMessage(), E_USER_WARNING);
            return -1;
        }
    }
}
?>
